/**
 * @Author: chenzehao
 * @Date: 2024-05-02
 *
 */
package com.sprint.questai.ocrhttp;

import cn.hutool.core.io.resource.ResourceUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Refresh;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.sprint.questai.factory.ModelFactory;
import com.sprint.questai.model.enums.NameEnums;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.segment.TextSegment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SpringBootTest
public class ocrtestAndSave {
    @Autowired  // Spring自动注入相应的组件
    ModelFactory modelFactory;  // 模型工厂，用于获取不同的模型实例
    @Resource
    private ElasticsearchClient client;
    @Autowired
    private  FileService fileService;
    @Resource
    private OCRServiceHttp ocrServiceHttp;

    @Test
    public void ocrTest() throws IOException {
        // MockMultipartFile用于模拟文件上传
        //MockMultipartFile file = new MockMultipartFile("file", "filename.txt", "text/plain", "Some content".getBytes());

        // 文件存储逻辑
        //String filePath = fileService.storeFile(file);

        URL resource = ResourceUtil.getResource("legal_data/PDF/test01.pdf");
        String filePath ="C:\\Users\\24649\\Desktop\\2.pdf";

        ResourceUtil.getResource("legal_data/PDF/test01.pdf");
        File file = new File(resource.getFile());
                // OCR处理逻辑
        String ocrResult = ocrServiceHttp.processFile(resource.getPath());
        System.out.println(ocrResult);
        // 创建分割器并分割文本
        DocumentSplitter splitter = modelFactory.createDocumentSplitter(NameEnums.DEFAULT_DOCUMENT_SPLITTER.getText());
        Document document = new Document(ocrResult);
        List<TextSegment> segments = splitter.split(document);

        // 将TextSegment转换为String
        List<String> chunks = segments.stream()
                .map(TextSegment::text)  // 使用text方法获取文本内容
                .collect(Collectors.toList());

        // JSON转换工具
        ObjectMapper objectMapper = new ObjectMapper();

        // 保存分割后的chunks到Elasticsearch
        String indexName = "my_index";
        for (String chunk : chunks) {
            // 创建一个包含文本内容的文档
            Map<String, String> jsonDocument = new HashMap<>();
            jsonDocument.put("content", chunk);

            // 索引文档到Elasticsearch
            IndexResponse response = client.index(i -> i
                    .index(indexName)
                    .document(jsonDocument)  // 传递Java对象而不是JSON字符串
                    .refresh(Refresh.True));  // 使用枚举类型而不是字符串
            System.out.println("Document indexed: ID=" + response.id());
        }
    }
}
